package cn.lds.healthexamspringboot.interceptor;

import cn.lds.healthexamspringboot.dto.HealthExamConstant;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();

        Object loginUser = session.getAttribute("login");

        if (null == loginUser){
            request.setAttribute("err", HealthExamConstant.UserMsg.REQUERY_LOGIN);
            request.getRequestDispatcher("/user/login").forward(request,response);
        }

        return true;

    }
}
